package com.tsingtao.arch.rule;

import static com.societegenerale.commons.plugin.utils.ArchUtils.importAllClassesInPackage;
import static com.tngtech.archunit.core.domain.JavaClass.Predicates.TOP_LEVEL_CLASSES;
import static com.tngtech.archunit.core.domain.JavaClass.Predicates.resideInAPackage;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;

import com.societegenerale.commons.plugin.rules.ArchRuleTest;
import com.societegenerale.commons.plugin.service.ScopePathProvider;
import com.tngtech.archunit.base.DescribedPredicate;
import com.tngtech.archunit.core.domain.JavaClass;
import java.util.Collection;

/**
 * Mybatis Mapper 规则检查
 */
public class MybatisMapperRulesTest implements ArchRuleTest {

  public static final DescribedPredicate<JavaClass> MYBATIS_MAPPER = resideInAPackage("..persistent..")
      .and(resideInAPackage("..mybatis.."));

  @Override
  public void execute(String packagePath, ScopePathProvider scopePathProvider, Collection<String> excludedPaths) {
    classes().that(TOP_LEVEL_CLASSES.and(MYBATIS_MAPPER)).and()
        .areAnnotatedWith("org.apache.ibatis.annotations.Mapper").should()
        .haveSimpleNameEndingWith("Mapper").andShould()
        .resideInAPackage("..persistent..").andShould()
        .resideInAPackage("..mybatis..")
        .check(importAllClassesInPackage(scopePathProvider.getMainClassesPath(), packagePath, excludedPaths));
  }

}